package yongxin.controller.check;


import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jeecgframework.core.util.ResourceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.service.SystemService;

import yongxin.entity.annealer.AnnealerEntity;
import yongxin.entity.check.ContractEntity;
import yongxin.entity.check.ContractProcutEntity;
import yongxin.entity.processreport.ProcessReportEntity;
import yongxin.entity.temp_record.TempRecordEntity;

/**   
 * @Title: Controller
 * @Description:  销售合同审批
 * @author zhangdaihao
 * @date 2015-02-05 14:03:13
 * @version V1.0   
 *
 */

@Controller
@RequestMapping("/saleContractCheckController")
public class SaleContractCheckController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(SaleContractCheckController.class);

	@Autowired
	private SystemService systemService;
	
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 销售合同列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "contract")
	public ModelAndView processReport(HttpServletRequest request) {
		
		return new ModelAndView("yongxin/check/contractList");
		
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(ProcessReportEntity processReport,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		

		        
	/*	 String sql =  " select * from  ";
				sql +="OPENQUERY ( RemoteServer , ";
				sql +="' ";*/
		String sql = "";
				sql +=" SELECT  ";
				sql +="t_RPContract.FContractID,t_RPContract.FBase6,t_Organization.FNumber AS FBase6_FNDName,t_Organization.FName AS FBase6_DSPName,t_RPContract.FBase,t_SubMessage.FID AS FBase_FNDName, ";
				sql +="t_SubMessage.FName AS FBase_DSPName,t_RPContract.FComboBox,t_RPContract.FCustomer,t_Item1.FNumber AS FCustomer_FNDName,t_Item1.FName AS FCustomer_DSPName,t_RPContract.FText1, ";
				sql +=" t_RPContract.FBase1,t_Settle.FNumber AS FBase1_FNDName,t_Settle.FName AS FBase1_DSPName,t_RPContract.FText2,t_RPContract.FBase2,t_SubMessage1.FID AS FBase2_FNDName, ";
				sql +=" t_SubMessage1.FName AS FBase2_DSPName,t_RPContract.FBase3,t_SubMessage2.FID AS FBase3_FNDName,t_SubMessage2.FName AS FBase3_DSPName,t_RPContract.FText3,t_RPContract.FText4, ";
				sql +=" t_RPContract.FDetail,t_RPContract.FContractTypeID,t_rp_systemenum.fname AS FContractTypeID_FNDName,t_rp_systemenum.fname AS FContractTypeID_DSPName,t_RPContract.FContractName,t_RPContract.Fstatus Fstatus1, ";
				sql +=" t_RPContract.Fstatus,t_RPContract.FDate,t_RPContract.FContractNo,t_RPContract.FItemClassID,t_ItemClass.FNumber AS FItemClassID_FNDName,t_ItemClass.FName AS FItemClassID_DSPName, ";
				sql +=" t_RPContract.FDepartment,t_Department.FNumber AS FDepartment_FNDName,t_Department.FName AS FDepartment_DSPName,t_RPContract.FEmployee,t_Emp.FNumber AS FEmployee_FNDName,t_Emp.FName AS FEmployee_DSPName, ";
				sql +=" t_RPContract.FCurrencyID,t_Currency.FNumber AS FCurrencyID_FNDName,t_Currency.FScale AS FCurrencyID_Decimal,t_Currency.FName AS FCurrencyID_DSPName,t_RPContract.FExchangerate,t_RPContract.FTotalAmountFor, ";
				sql +=" t_RPContract.FTotalAmount,t_RPContract.FCashDiscount,t_RPContractScheme.FCurrencyID AS FCurrencyID2,t_RPContractScheme.FExchangeRate AS FExchangeRate2,t_RPContract.FExplanation,t_RPContract.FText, ";
				sql +=" t_RPContract.FBillerID,t_User1.FName AS FBillerID_FNDName,t_User1.FName AS FBillerID_DSPName,t_RPContract.FChecker,t_User.FName AS FChecker_FNDName,t_User.FName AS FChecker_DSPName, ";
				sql +=" t_RPContract.FCheckDate,t_RPContract.FTaskID,vw_rp_PMTask.FNumber AS FTaskID_FNDName,vw_rp_PMTask.FName AS FTaskID_DSPName,t_RPContract.FResourceID,vw_rp_PMResource.FNumber AS FResourceID_FNDName, ";
				sql +=" vw_rp_PMResource.FName AS FResourceID_DSPName,t_RPContract.FBudgetAmountFor,t_RPContract.FOrderID,vw_rp_PMOrder.FNumber AS FOrderID_FNDName,vw_rp_PMOrder.FName AS FOrderID_DSPName,t_RPContract.FTotalAmountFor_Commit, ";
				sql +=" t_RPContract.FOrderID AS FOrderAmountFor,vw_rp_PMOrder.FAmountFor AS FOrderAmountFor_DSPName,t_RPContractScheme.FReceiveDate,t_RPContractScheme.FAmountFor,t_RPContractScheme.FAmount AS FAmount2,t_RPContract.FRP, ";
				sql +=" t_RPContract.FReceiveAmount,t_RPContract.FReceiveAmountFor,t_RPContract.FSystemType,t_rpContract.FInvoiceAmtFor_Relative AS FInvoiceAmtFor_Relative_1,t_RPContract.FUnInvoice_AmountFor AS FUnInvoice_AmountFor_1,t_RPContract.FInvoiceAmt_Relative AS FInvoiceAmt_Relative_1, ";
				sql +=" t_RPContract.FUnReceive_AmountFor AS FUnReceive_AmountFor_1,t_RPContract.FUnInvoice_Amount AS FUnInvoice_Amount_1,t_RPContract.FClassTypeID,t_RPContract.FUnReceive_Amount AS FUnReceive_Amount_1,t_RPContract.FIsInit,t_RPContractScheme.FEntryID AS FEntryID2, ";
				sql +=" t_RPContractScheme.FAmountFor_Commit,t_RPContractScheme.FContractID AS FContractID2,t_RPContractScheme.FIndex AS FIndex2   ";
				
				sql += " ,t_ICItem1.FName AS FItemID40828_DSPName,t_ICItem1.FModel AS FItemID40891_DSPName,t_rpContractEntry.FText6,t_rpContractEntry.FText5,t_SubMessage3.FName AS FBase4_DSPName, ";
				sql += " t_rpContractEntry.FDate1,t_rpContractEntry.FQuantity,t_rpContractEntry.FTaxPriceFor,t_rpContractEntry.FAmountIncludeTaxFor ";
				
				
				
				
				sql +=" FROM  AIS2009.dbo.t_RPContract t_RPContract  INNER JOIN AIS2009.dbo.t_RPContractScheme t_RPContractScheme  ON t_RPContract.FContractID=t_RPContractScheme.FContractID ";
				sql +=" LEFT  JOIN AIS2009.dbo.t_rp_systemenum t_rp_systemenum  ON t_RPContract.FContractTypeID=t_rp_systemenum.fitemid ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_ITem t_Item1 ON t_RPContract.FCustomer=t_Item1.FItemID AND t_Item1.FItemID<>0 ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_Currency t_Currency  ON t_RPContract.FCurrencyID=t_Currency.FCurrencyID AND t_Currency.FCurrencyID<>0 ";
				sql +=" LEFT  JOIN AIS2009.dbo.vw_rp_PMTask vw_rp_PMTask  ON t_RPContract.FTaskID=vw_rp_PMTask.FID ";
				sql +="  LEFT  JOIN AIS2009.dbo.vw_rp_PMResource vw_rp_PMResource  ON t_RPContract.FResourceID=vw_rp_PMResource.FID ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_ItemClass t_ItemClass  ON t_RPContract.FItemClassID=t_ItemClass.FItemClassID AND t_ItemClass.FItemClassID<>0 ";
				sql +="  LEFT  JOIN AIS2009.dbo.vw_rp_PMOrder vw_rp_PMOrder  ON t_RPContract.FOrderID=vw_rp_PMOrder.FID ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_Department t_Department  ON t_RPContract.FDepartment=t_Department.FItemID AND t_Department.FItemID<>0 ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_Emp t_Emp  ON t_RPContract.FEmployee=t_Emp.FItemID AND t_Emp.FItemID<>0 ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_User t_User  ON t_RPContract.FChecker=t_User.FUserID AND t_User.FUserID<>0 ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_User t_User1 ON t_RPContract.FBillerID=t_User1.FUserID AND t_User1.FUserID<>0 ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_SubMessage t_SubMessage  ON t_RPContract.FBase=t_SubMessage.FInterID AND t_SubMessage.FInterID<>0 ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_Settle t_Settle  ON t_RPContract.FBase1=t_Settle.FItemID AND t_Settle.FItemID<>0 ";
				sql +=" LEFT  JOIN AIS2009.dbo.t_SubMessage t_SubMessage1 ON t_RPContract.FBase2=t_SubMessage1.FInterID AND t_SubMessage1.FInterID<>0 ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_SubMessage t_SubMessage2 ON t_RPContract.FBase3=t_SubMessage2.FInterID AND t_SubMessage2.FInterID<>0 ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_Organization t_Organization  ON t_RPContract.FBase6=t_Organization.FItemID AND t_Organization.FItemID<>0 ";
				
				
				sql +="  INNER JOIN AIS2009.dbo.t_rpContractEntry t_rpContractEntry  ON t_rpContract.FContractID = t_rpContractEntry.FContractID ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_ICItem t_ICItem1 ON t_rpContractEntry.FProductID=t_ICItem1.FItemID AND t_ICItem1.FItemID<>0  ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_SubMessage t_SubMessage3 ON t_rpContractEntry.FBase4=t_SubMessage3.FInterID AND t_SubMessage3.FInterID<>0 "; 
				sql +="  LEFT  JOIN AIS2009.dbo.t_SubMessage t_SubMessage4 ON t_rpContractEntry.FBase5=t_SubMessage4.FInterID AND t_SubMessage4.FInterID<>0 "; 
				sql +="  LEFT  JOIN AIS2009.dbo.t_Measureunit t_Measureunit ON t_rpContractEntry.FUnitID=t_Measureunit.FItemID AND t_Measureunit.FItemID<>0 "; 
				sql +="  LEFT  JOIN AIS2009.dbo.t_Item t_ICItem1_FBaseUnitID ON t_ICItem1.FUnitID=t_ICItem1_FBaseUnitID.FItemID AND t_ICItem1_FBaseUnitID.FItemID<>0  ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_AuxItem t_AuxItem ON t_rpContractEntry.FAuxPropID=t_AuxItem.FItemID AND t_AuxItem.FItemID<>0 "; 
				sql +="  LEFT  JOIN AIS2009.dbo.t_Measureunit t_Measureunit1 ON t_RpContractEntry.FSecUnitID=t_Measureunit1.FItemID AND t_Measureunit1.FItemID<>0 "; 
				
				
				sql +="  WHERE t_RPContract.FContractID in (select fbillid ";
				sql +=" from AIS2009.dbo.ICClassCheckStatus ";
				sql +=" where  fcurrentlevel = 1 and FClassTypeID = 1000019)";
				//sql +=" 	 ') ";
				
				// 排序
				if(dataGrid.getSort() != null)
				{
					String orderField = dataGrid.getSort();
					if(dataGrid.getSort().equals(orderField)){

							sql += " order by " + orderField + dataGrid.getOrder();

					}
				}
				
				List<Map<String, Object>> maplist = null;
				List<Map<String, Object>> allmaplist = null;

				try
				{

					allmaplist = systemService.findForJdbc(sql);
					maplist = systemService.findForJdbcParam(sql, dataGrid.getPage(), dataGrid.getRows());

				} catch (Exception ex)
				{

				}
		 
				dataGrid.setTotal(allmaplist.size());
				dataGrid.setResults(maplist);
				TagUtil.datagrid(response, dataGrid);
		
		
	}


	
	/**
	 * 工序汇报列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(HttpServletRequest req) {
		
			String FContractID = req.getParameter("FContractID");
			int id = Integer.parseInt(FContractID);
		    Object[] object=new Object[1];
		    object[0]=id;
		 String sql=  " select * from  ";
				sql +="OPENQUERY ( RemoteServer , ";
				sql +="' ";
				sql +=" SELECT  ";
				sql +="t_RPContract.FContractID,t_RPContract.FBase6,t_Organization.FNumber AS FBase6_FNDName,t_Organization.FName AS FBase6_DSPName,t_RPContract.FBase,t_SubMessage.FID AS FBase_FNDName, ";
				sql +="t_SubMessage.FName AS FBase_DSPName,t_RPContract.FComboBox,t_RPContract.FCustomer,t_Item1.FNumber AS FCustomer_FNDName,t_Item1.FName AS FCustomer_DSPName,t_RPContract.FText1, ";
				sql +=" t_RPContract.FBase1,t_Settle.FNumber AS FBase1_FNDName,t_Settle.FName AS FBase1_DSPName,t_RPContract.FText2,t_RPContract.FBase2,t_SubMessage1.FID AS FBase2_FNDName, ";
				sql +=" t_SubMessage1.FName AS FBase2_DSPName,t_RPContract.FBase3,t_SubMessage2.FID AS FBase3_FNDName,t_SubMessage2.FName AS FBase3_DSPName,t_RPContract.FText3,t_RPContract.FText4, ";
				sql +=" t_RPContract.FDetail,t_RPContract.FContractTypeID,t_rp_systemenum.fname AS FContractTypeID_FNDName,t_rp_systemenum.fname AS FContractTypeID_DSPName,t_RPContract.FContractName,t_RPContract.Fstatus Fstatus1, ";
				sql +=" t_RPContract.Fstatus,t_RPContract.FDate,t_RPContract.FContractNo,t_RPContract.FItemClassID,t_ItemClass.FNumber AS FItemClassID_FNDName,t_ItemClass.FName AS FItemClassID_DSPName, ";
				sql +=" t_RPContract.FDepartment,t_Department.FNumber AS FDepartment_FNDName,t_Department.FName AS FDepartment_DSPName,t_RPContract.FEmployee,t_Emp.FNumber AS FEmployee_FNDName,t_Emp.FName AS FEmployee_DSPName, ";
				sql +=" t_RPContract.FCurrencyID,t_Currency.FNumber AS FCurrencyID_FNDName,t_Currency.FScale AS FCurrencyID_Decimal,t_Currency.FName AS FCurrencyID_DSPName,t_RPContract.FExchangerate,t_RPContract.FTotalAmountFor, ";
				sql +=" t_RPContract.FTotalAmount,t_RPContract.FCashDiscount,t_RPContractScheme.FCurrencyID AS FCurrencyID2,t_RPContractScheme.FExchangeRate AS FExchangeRate2,t_RPContract.FExplanation,t_RPContract.FText, ";
				sql +=" t_RPContract.FBillerID,t_User1.FName AS FBillerID_FNDName,t_User1.FName AS FBillerID_DSPName,t_RPContract.FChecker,t_User.FName AS FChecker_FNDName,t_User.FName AS FChecker_DSPName, ";
				sql +=" t_RPContract.FCheckDate,t_RPContract.FTaskID,vw_rp_PMTask.FNumber AS FTaskID_FNDName,vw_rp_PMTask.FName AS FTaskID_DSPName,t_RPContract.FResourceID,vw_rp_PMResource.FNumber AS FResourceID_FNDName, ";
				sql +=" vw_rp_PMResource.FName AS FResourceID_DSPName,t_RPContract.FBudgetAmountFor,t_RPContract.FOrderID,vw_rp_PMOrder.FNumber AS FOrderID_FNDName,vw_rp_PMOrder.FName AS FOrderID_DSPName,t_RPContract.FTotalAmountFor_Commit, ";
				sql +=" t_RPContract.FOrderID AS FOrderAmountFor,vw_rp_PMOrder.FAmountFor AS FOrderAmountFor_DSPName,t_RPContractScheme.FReceiveDate,t_RPContractScheme.FAmountFor,t_RPContractScheme.FAmount AS FAmount2,t_RPContract.FRP, ";
				sql +=" t_RPContract.FReceiveAmount,t_RPContract.FReceiveAmountFor,t_RPContract.FSystemType,t_rpContract.FInvoiceAmtFor_Relative AS FInvoiceAmtFor_Relative_1,t_RPContract.FUnInvoice_AmountFor AS FUnInvoice_AmountFor_1,t_RPContract.FInvoiceAmt_Relative AS FInvoiceAmt_Relative_1, ";
				sql +=" t_RPContract.FUnReceive_AmountFor AS FUnReceive_AmountFor_1,t_RPContract.FUnInvoice_Amount AS FUnInvoice_Amount_1,t_RPContract.FClassTypeID,t_RPContract.FUnReceive_Amount AS FUnReceive_Amount_1,t_RPContract.FIsInit,t_RPContractScheme.FEntryID AS FEntryID2, ";
				sql +=" t_RPContractScheme.FAmountFor_Commit,t_RPContractScheme.FContractID AS FContractID2,t_RPContractScheme.FIndex AS FIndex2   ";
				sql +=" FROM  AIS2009.dbo.t_RPContract t_RPContract  INNER JOIN AIS2009.dbo.t_RPContractScheme t_RPContractScheme  ON t_RPContract.FContractID=t_RPContractScheme.FContractID ";
				sql +=" LEFT  JOIN AIS2009.dbo.t_rp_systemenum t_rp_systemenum  ON t_RPContract.FContractTypeID=t_rp_systemenum.fitemid ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_ITem t_Item1 ON t_RPContract.FCustomer=t_Item1.FItemID AND t_Item1.FItemID<>0 ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_Currency t_Currency  ON t_RPContract.FCurrencyID=t_Currency.FCurrencyID AND t_Currency.FCurrencyID<>0 ";
				sql +=" LEFT  JOIN AIS2009.dbo.vw_rp_PMTask vw_rp_PMTask  ON t_RPContract.FTaskID=vw_rp_PMTask.FID ";
				sql +="  LEFT  JOIN AIS2009.dbo.vw_rp_PMResource vw_rp_PMResource  ON t_RPContract.FResourceID=vw_rp_PMResource.FID ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_ItemClass t_ItemClass  ON t_RPContract.FItemClassID=t_ItemClass.FItemClassID AND t_ItemClass.FItemClassID<>0 ";
				sql +="  LEFT  JOIN AIS2009.dbo.vw_rp_PMOrder vw_rp_PMOrder  ON t_RPContract.FOrderID=vw_rp_PMOrder.FID ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_Department t_Department  ON t_RPContract.FDepartment=t_Department.FItemID AND t_Department.FItemID<>0 ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_Emp t_Emp  ON t_RPContract.FEmployee=t_Emp.FItemID AND t_Emp.FItemID<>0 ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_User t_User  ON t_RPContract.FChecker=t_User.FUserID AND t_User.FUserID<>0 ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_User t_User1 ON t_RPContract.FBillerID=t_User1.FUserID AND t_User1.FUserID<>0 ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_SubMessage t_SubMessage  ON t_RPContract.FBase=t_SubMessage.FInterID AND t_SubMessage.FInterID<>0 ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_Settle t_Settle  ON t_RPContract.FBase1=t_Settle.FItemID AND t_Settle.FItemID<>0 ";
				sql +=" LEFT  JOIN AIS2009.dbo.t_SubMessage t_SubMessage1 ON t_RPContract.FBase2=t_SubMessage1.FInterID AND t_SubMessage1.FInterID<>0 ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_SubMessage t_SubMessage2 ON t_RPContract.FBase3=t_SubMessage2.FInterID AND t_SubMessage2.FInterID<>0 ";
				sql +="  LEFT  JOIN AIS2009.dbo.t_Organization t_Organization  ON t_RPContract.FBase6=t_Organization.FItemID AND t_Organization.FItemID<>0 ";
				sql +="  WHERE t_RPContract.FContractID = %s ";
				sql +=" 	 ') ";
				
				
				sql=String.format(sql, id);  
				
				
			// 根据	FContractID  查询合同信息
				List<Map<String,Object>>  mapList= systemService.findForJdbc(sql);
				ContractEntity contractEntity = new ContractEntity();
				for (int i=0;i<mapList.size();i++)
				{
				       Map  map=(Map)mapList.get(i);
				       contractEntity.setFbaseDspname((String) map.get("FBase_DSPName"));
				       contractEntity.setFreceivedate((Date) map.get("FReceiveDate"));
				       contractEntity.setFcurrencyidDspname((String) map.get("FCurrencyID_DSPName"));
				       contractEntity.setFcontractno((String) map.get("FContractNo"));
				       contractEntity.setFexchangerate((Double) map.get("FExchangerate"));
				       contractEntity.setFitemclassidDspname((String) map.get("FItemClassID_DSPName"));
				       contractEntity.setFbase6Dspname((String) map.get("FBase6_DSPName"));
				       
				       contractEntity.setFtotalamountfor( (BigDecimal) map.get("FTotalAmountFor"));
				       contractEntity.setFtotalamount( (BigDecimal) map.get("FTotalAmount"));
				       contractEntity.setFbase1Dspname((String) map.get("FBase1_DSPName"));
				       contractEntity.setFtext2((String) map.get("FText2"));
				       contractEntity.setFbase2Dspname((String) map.get("FBase2_DSPName"));
				       contractEntity.setFtext3((String) map.get("FText3"));
				       contractEntity.setFbase3Dspname((String) map.get("FBase3_DSPName"));
				       
				       contractEntity.setFdepartmentDspname((String) map.get("FDepartment_DSPName"));
				       contractEntity.setFcheckerDspname((String) map.get("FChecker_DSPName"));
				       contractEntity.setFemployeeDspname((String) map.get("FEmployee_DSPName"));
				       contractEntity.setFcheckdate((Date) map.get("FCheckDate"));
				       contractEntity.setFbilleridDspname((String) map.get("FBillerID_DSPName"));
				       contractEntity.setFtext((String) map.get("Ftext"));
				}
				contractEntity.setId(String.valueOf(id));
				req.setAttribute("contractPage", contractEntity);
			
				
			String sql2 = "	select * from ";
			sql2 += " OPENQUERY ( RemoteServer ,";
 			sql2 += " ' ";
			sql2 += " SELECT ";
			sql2 += " t_rpContractEntry.FInvoiceQty_Commit,t_rpContractEntry.FPrice,t_rpContractEntry.FTaxPrice,t_rpContractEntry.FProductID AS FItemID40828,t_ICItem1.FName AS FItemID40828_DSPName,t_rpContractEntry.FProductID AS FItemID40891, ";
			sql2 += " t_ICItem1.FModel AS FItemID40891_DSPName,t_rpContractEntry.FAuxPropID,t_AuxItem.FNumber AS FAuxPropID_FNDName,t_AuxItem.FName AS FAuxPropID_DSPName,t_rpContractEntry.FUnitID,t_Measureunit.FNumber AS FUnitID_FNDName, ";
			sql2 += " t_Measureunit.FName AS FUnitID_DSPName,t_rpContractEntry.FQuantity,t_rpContractEntry.FTaxPriceFor,t_rpContractEntry.FPriceFor,t_rpContractEntry.FProductID AS FProductID2,t_ICItem1.FNumber AS FProductID2_FNDName, ";
			sql2 += " t_ICItem1.FTrack AS FProductID2_Track,t_ICItem1.FBatchManager AS FProductID2_BatchNoManage,t_ICItem1.FQtyDecimal AS FProductID2_FQtyDecimal,t_ICItem1.FPriceDecimal AS FProductID2_FPriceDecimal,t_ICItem1.FUnitGroupID AS FProductID2_UnitGroupID,t_ICItem1.FISKFPeriod AS FProductID2_FISKFPeriod, ";
			sql2 += " t_ICItem1.FNumber AS FProductID2_DSPName,t_rpContractEntry.FBusinessDiscountRate,t_rpContractEntry.FFactPriceFor,t_rpContractEntry.FBusinessDiscountFor,t_rpContractEntry.FBusinessDiscount,t_rpContractEntry.FAmountIncludeTaxFor, ";
			sql2 += " t_rpContractEntry.FAmountIncludeTax,t_rpContractEntry.FTaxRate,t_rpContractEntry.FTaxFor,t_rpContractEntry.FTax,t_rpContractEntry.FAmountFor AS FAmountFor3,t_rpContractEntry.FAmount, ";
			sql2 += " t_rpContractEntry.FExplanation AS FExplanation_3,t_RpContractEntry.FsecCoefficient,t_RpContractEntry.FSecUnitID,t_Measureunit1.FNumber AS FSecUnitID_FNDName,t_Measureunit1.FName AS FSecUnitID_DSPName,t_RpContractEntry.FSecQty, ";
			sql2 += " t_rpContractEntry.FProductID AS FBaseUnitID,t_ICItem1_FBaseUnitID.FName AS FBaseUnitID_DSPName,t_rpContractEntry.FQuantity_Base,t_rpContractEntry.FOrderQty_Relative,t_rpContractEntry.FOrderQty_Relative_Base,t_rpContractEntry.FInvoiceQty_Relative, ";
			sql2 += " t_rpContractEntry.FText6,t_rpContractEntry.FText5,t_rpContractEntry.FBase4,t_SubMessage3.FID AS FBase4_FNDName,t_SubMessage3.FName AS FBase4_DSPName,t_rpContractEntry.FBase5, ";
			sql2 += " t_SubMessage4.FID AS FBase5_FNDName,t_SubMessage4.FName AS FBase5_DSPName,t_rpContractEntry.FInvoiceQty_Relative_Base,t_rpContractEntry.FInvoiceAmtFor_Relative,t_rpContractEntry.FInvoiceAmt_Relative,t_rpContractEntry.FEntryID AS FEntryID3, ";
			sql2 += " t_rpContractEntry.FBillQty_Relative,t_rpContractEntry.FReceiveAmountFor AS FReceiveAmountFor_3,t_rpContractEntry.FContractID AS FContractID3,t_rpContractEntry.FDate1,t_rpContractEntry.FReceiveAmount AS FReceiveAmount_3,t_rpContractEntry.FUnInvoice_Qty, ";
			sql2 += " t_rpContractEntry.FUnInvoice_AmountFor,t_rpContractEntry.FUnReceive_Qty,t_rpContractEntry.FDecimal,t_rpContractEntry.FUnReceive_AmountFor,t_rpContractEntry.FDecimal1,t_rpContractEntry.FDecimal3, ";
			sql2 += " t_rpContractEntry.FUnInvoice_Amount,t_rpContractEntry.FDecimal2,t_rpContractEntry.FUnReceive_Amount,t_rpContractEntry.FIndex AS FIndex3  ";
			sql2 += "  FROM AIS2009.dbo.t_rpContractEntry t_rpContractEntry LEFT  JOIN AIS2009.dbo.t_ICItem t_ICItem1 ON t_rpContractEntry.FProductID=t_ICItem1.FItemID AND t_ICItem1.FItemID<>0 ";
			sql2 += "  LEFT  JOIN AIS2009.dbo.t_SubMessage t_SubMessage3 ON t_rpContractEntry.FBase4=t_SubMessage3.FInterID AND t_SubMessage3.FInterID<>0 ";
			sql2 += "  LEFT  JOIN AIS2009.dbo.t_SubMessage t_SubMessage4 ON t_rpContractEntry.FBase5=t_SubMessage4.FInterID AND t_SubMessage4.FInterID<>0 ";
			sql2 += "  LEFT  JOIN AIS2009.dbo.t_Measureunit t_Measureunit ON t_rpContractEntry.FUnitID=t_Measureunit.FItemID AND t_Measureunit.FItemID<>0 ";
			sql2 += "  LEFT  JOIN AIS2009.dbo.t_Item t_ICItem1_FBaseUnitID ON t_ICItem1.FUnitID=t_ICItem1_FBaseUnitID.FItemID AND t_ICItem1_FBaseUnitID.FItemID<>0 ";
			sql2 += "  LEFT  JOIN AIS2009.dbo.t_AuxItem t_AuxItem ON t_rpContractEntry.FAuxPropID=t_AuxItem.FItemID AND t_AuxItem.FItemID<>0 ";
			sql2 += "  LEFT  JOIN AIS2009.dbo.t_Measureunit t_Measureunit1 ON t_RpContractEntry.FSecUnitID=t_Measureunit1.FItemID AND t_Measureunit1.FItemID<>0 ";
			sql2 += "  LEFT  JOIN AIS2009.dbo.t_rpContract t_rpContract ON t_rpContract.FContractID = t_rpContractEntry.FContractID  ";
			sql2 += "  WHERE t_rpContractEntry.FContractID = %s ";
			sql2 += "  ') "; 
            
			sql2=String.format(sql2, id);
			// 根据	FContractID  查询产品信息
			List<Map<String,Object>>  mapList2= systemService.findForJdbc(sql2);
			List<ContractProcutEntity> list = new ArrayList<ContractProcutEntity>();
			
			// 数量和价税合计
			Double sum_qty = 0.00;
			BigDecimal sum_FAmountIncludeTaxFor  = new BigDecimal("0.0000");
			for (int i=0;i<mapList2.size();i++)
			{      
			       Map  map=(Map)mapList2.get(i);
			       ContractProcutEntity contractProcutEntity = new ContractProcutEntity();
			       contractProcutEntity.setFproductid2Fndname((String) map.get("FProductID2_FNDName"));
			       contractProcutEntity.setFitemid40828Dspname((String) map.get("FItemID40828_DSPName"));
			       contractProcutEntity.setFitemid40891Dspname((String) map.get("FItemID40891_DSPName"));
			       contractProcutEntity.setFtext6((String) map.get("FText6"));
			       contractProcutEntity.setFtext5((String) map.get("FText5"));
			       contractProcutEntity.setFbase4Dspname((String) map.get("FBase4_DSPName"));
			       contractProcutEntity.setFdecimal((BigDecimal) map.get("Fdecimal"));
			       contractProcutEntity.setFdecimal1( (BigDecimal) map.get("FDecimal1"));
			       contractProcutEntity.setFdecimal3( (BigDecimal) map.get("FDecimal3"));
			       contractProcutEntity.setFdecimal2( (BigDecimal) map.get("FDecimal2"));
			       contractProcutEntity.setFbase5Dspname((String) map.get("FBase5_DSPName"));
			       contractProcutEntity.setFdate1((Date) map.get("FDate1"));
			       contractProcutEntity.setFunitidDspname((String) map.get("FUnitID_DSPName"));
			       contractProcutEntity.setFquantity( (Double) map.get("Fquantity"));
			       contractProcutEntity.setFtaxpricefor( (Double)  map.get("FTaxPriceFor"));
			       contractProcutEntity.setFamountincludetaxfor( (BigDecimal) map.get("FAmountIncludeTaxFor"));
			       
			       contractProcutEntity.setFamountincludetax( (BigDecimal) map.get("FAmountIncludeTaxFor"));
			       contractProcutEntity.setFtaxrate( (Double) map.get("FTaxRate"));
			       contractProcutEntity.setFtaxfor(  (BigDecimal) map.get("FTaxFor"));
			       contractProcutEntity.setFtax(  (BigDecimal) map.get("Ftax"));
			       contractProcutEntity.setFamountfor3( (BigDecimal) map.get("FAmountFor3"));
			       contractProcutEntity.setFamount( (BigDecimal) map.get("Famount"));
			       contractProcutEntity.setFpricefor(  (Double) map.get("FPriceFor"));
			       contractProcutEntity.setFbusinessdiscountrate( (Double) map.get("FBusinessDiscountRate"));
			       contractProcutEntity.setFfactpricefor( (Double) map.get("FFactPriceFor"));
			       contractProcutEntity.setFbusinessdiscountfor( (BigDecimal) map.get("FBusinessDiscountFor"));
			       contractProcutEntity.setFbusinessdiscount( (BigDecimal) map.get("FBusinessDiscount"));
			       contractProcutEntity.setFbaseunitidDspname((String) map.get("FBaseUnitID_DSPName"));
			       contractProcutEntity.setFquantityBase( (BigDecimal) map.get("FQuantity_Base"));
			      
			       sum_qty += contractProcutEntity.getFquantity();
			       sum_FAmountIncludeTaxFor = sum_FAmountIncludeTaxFor.add(contractProcutEntity.getFamountincludetaxfor());
			       
			       list.add(contractProcutEntity);
			}
			req.setAttribute("productList", list);
			req.setAttribute("sum_qty", sum_qty);
			req.setAttribute("sum_FAmountIncludeTaxFor", sum_FAmountIncludeTaxFor);
			
			return new ModelAndView("yongxin/check/saleConractDetail");
		
	}
	
	
	/**
	 * 审核 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "check")
	public ModelAndView check(HttpServletRequest req) {
		
		String FContractID = req.getParameter("id");
		String FContractNo = req.getParameter("no");
		req.setAttribute("id", FContractID);
		req.setAttribute("no", FContractNo);
		return new ModelAndView("yongxin/check/check_idea");
		
	}
	
	@RequestMapping(params = "uncheck")
	public ModelAndView uncheck(HttpServletRequest req) {
		
		String FContractID = req.getParameter("id");
		String FContractNo = req.getParameter("no");
		req.setAttribute("id", FContractID);
		req.setAttribute("no", FContractNo);
		return new ModelAndView("yongxin/check/uncheck_idea");
		
	}
	/**
	 * 审核
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "saveCheckIdea")
	@ResponseBody
	public AjaxJson saveCheckIdea(HttpServletRequest request) {
		
		AjaxJson j = new AjaxJson();
		String FContractNo = request.getParameter("no");
		String FContractID = request.getParameter("id");
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		String checkDate = formatter.format(new Date());
		
		String userId = "";
		//获取登录用户
		TSUser user = ResourceUtil.getSessionUser();//(TSUser) request.getSession().getAttribute("loginUser");
		if(user.getUserName().equals("yuanxl")){
			
			userId = "16446";
			
		}else{
			
			userId = "16501";
		}
		
	   String sql = " SET XACT_ABORT ON ; update RemoteServer.AIS2009.dbo.ICClassCheckStatus ";                         
		      sql += " set fcheckman2 = ?,fcheckidea2 = ?,fcheckdate2 = ?,fcurrentLevel=3 ";
		      sql += " where fbillid = ? ";
	   
	  String sql2  = " SET XACT_ABORT ON ;update OPENQUERY(RemoteServer,'select fstatus,fchecker,FCheckDate,FContractID from AIS2009.dbo.t_RPContract') ";
	         sql2 += " set fstatus = 1,fchecker=?,FCheckDate= ? where FContractID = ? ";
	  
		String sql3 =  " SET XACT_ABORT ON ;insert RemoteServer.AIS2009.dbo.ICClassCheckRecords( ";
		       sql3 += " FClassTypeID,FPage,FBillID,FBillEntryID,FBillNo, FBillEntryIndex,FCheckLevel, ";
		       sql3 += " FCheckLevelTo,FMode,FCheckMan, FCheckIdea,FCheckDate,FDescriptions ) ";
		       sql3 += " Values (1000019,1,?,0,?,0,1,2,0,?,?,?,'审核')";
	                
              systemService.executeSql(sql,userId,"",checkDate,FContractID);
              systemService.executeSql(sql2,userId,checkDate,FContractID);
              systemService.executeSql(sql3,FContractID,FContractNo,userId,"",checkDate);
              
		      message = "合同号" + FContractNo + "审批成功";
		
		j.setMsg(message);
		return j;
	}
	/**
	 * 审核
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "saveUnCheckIdea")
	@ResponseBody
	public AjaxJson saveUnCheckIdea(HttpServletRequest request) {
		
		AjaxJson j = new AjaxJson();
		String FContractNo = request.getParameter("no");
		String FContractID = request.getParameter("id");
		String checkIdea = request.getParameter("checkIdea");
		checkIdea = checkIdea == null ? "" :checkIdea;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		String checkDate = formatter.format(new Date());
		String userId = "";
		//获取登录用户
		TSUser user = ResourceUtil.getSessionUser();//(TSUser) request.getSession().getAttribute("loginUser");
		if(user.getUserName().equals("yuanxl")){
			
			userId = "16446";
			
		}else{
			
			userId = "16501";
		}
		
   String sql = " SET XACT_ABORT ON ; update RemoteServer.AIS2009.dbo.ICClassCheckStatus ";                         
	      sql += " set fcheckman1 = '0',fcheckidea1 = '',fcheckdate1 = null,fcheckman2 = '0',fcheckidea2 = '',fcheckdate2 = null,fcurrentLevel=0 ";
	      sql += " where fbillid = ?";
		  
	String sql2 =  " SET XACT_ABORT ON ;insert RemoteServer.AIS2009.dbo.ICClassCheckRecords( ";
	       sql2 += " FClassTypeID,FPage,FBillID,FBillEntryID,FBillNo, FBillEntryIndex,FCheckLevel, ";
	       sql2 += " FCheckLevelTo,FMode,FCheckMan, FCheckIdea,FCheckDate,FDescriptions ) ";
	       sql2 += " Values (1000019,1,?,0,?,0,-1,-1,2,?,?,?,'驳回')"; 
	       
	      systemService.executeSql(sql,FContractID);
	      systemService.executeSql(sql2,FContractID,FContractNo,userId,checkIdea,checkDate);
		   message = "驳回成功";
		
		j.setMsg(message);
		return j;
	}
}

